import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "@/views/login";
import home from "@/views/chat";
import chat from "@/system/chat";
import wallet from "@/views/wallet";
import doc from "@/views/doc";
import selectedPictureCollections from "@/views/selectedPictureCollections";
import help from "@/views/help";
import share from "@/views/share";
import updateLog from "@/views/updateLog";
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:chat.loginPath
  },
  {
    path: chat.loginPath,
    name: 'login',
    component: login
  },
  {
    path: chat.homePath,
    name: 'home',
    component: home
  },
  {
    path: '/recharge',
    name: 'Recharge',
    component: wallet
  },
  {
    path: '/doc',
    name: 'doc',
    component: doc
  },
  {
    path: '/picture',
    name: 'picture',
    component: selectedPictureCollections
  },
  {
    path: '/help',
    name: 'help',
    component: help
  },
  {
    path: '/share/:code',
    name: 'share',
    component: share
  },
  {
    path: '/updateLog',
    name: 'updateLog',
    component: updateLog
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  let token = localStorage.getItem("token");
  if(to.path === '/' || to.path === '/auth/login'){
    if (token){
      return next("/chat")
    }else {
      return next()
    }
  }
  if (to.path === '/doc' || to.path === '/picture' || to.path.indexOf('/share/')!== -1){
    return next()
  }
  if (!token){
    return next("/auth/login")
  }
  next()
})

export default router
